package com.lxmes.device.service.impl;

import com.lxmes.common.exception.ServiceException;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.device.domain.NcEmTaskRecord;
import com.lxmes.device.mapper.NcEmTaskRecordMapper;
import com.lxmes.device.service.INcEmTaskRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 任务记录Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-10
 */
@Service
public class NcEmTaskRecordServiceImpl implements INcEmTaskRecordService
{
    @Autowired
    private NcEmTaskRecordMapper ncEmTaskRecordMapper;

    @Autowired
    protected Validator validator;

    /**
     * 查询任务记录
     *
     * @param id 任务记录主键
     * @return 任务记录
     */
    @Override
    public NcEmTaskRecord selectNcEmTaskRecordById(Long id)
    {
        return ncEmTaskRecordMapper.selectNcEmTaskRecordById(id);
    }

    /**
     * 查询任务记录列表
     *
     * @param ncEmTaskRecord 任务记录
     * @return 任务记录
     */
    @Override
    public List<NcEmTaskRecord> selectNcEmTaskRecordList(NcEmTaskRecord ncEmTaskRecord)
    {
        return ncEmTaskRecordMapper.selectNcEmTaskRecordList(ncEmTaskRecord);
    }

    /**
     * 新增任务记录
     *
     * @param ncEmTaskRecord 任务记录
     * @return 结果
     */
    @Override
    public int insertNcEmTaskRecord(NcEmTaskRecord ncEmTaskRecord)
    {
        ncEmTaskRecord.setCreateTime(DateUtils.getNowDate());
        return ncEmTaskRecordMapper.insertNcEmTaskRecord(ncEmTaskRecord);
    }

    /**
     * 修改任务记录
     *
     * @param ncEmTaskRecord 任务记录
     * @return 结果
     */
    @Override
    public int updateNcEmTaskRecord(NcEmTaskRecord ncEmTaskRecord)
    {
        ncEmTaskRecord.setUpdateTime(DateUtils.getNowDate());
        return ncEmTaskRecordMapper.updateNcEmTaskRecord(ncEmTaskRecord);
    }

    /**
     * 批量删除任务记录
     *
     * @param ids 需要删除的任务记录主键
     * @return 结果
     */
    @Override
    public int deleteNcEmTaskRecordByIds(Long[] ids)
    {
        return ncEmTaskRecordMapper.deleteNcEmTaskRecordByIds(ids);
    }

    /**
     * 删除任务记录信息
     *
     * @param id 任务记录主键
     * @return 结果
     */
    @Override
    public int deleteNcEmTaskRecordById(Long id)
    {
        return ncEmTaskRecordMapper.deleteNcEmTaskRecordById(id);
    }
    @Override
    public String imporIteminfo(List<NcEmTaskRecord> ncEmTaskRecordList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncEmTaskRecordList) || ncEmTaskRecordList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcEmTaskRecord ncEmTaskRecord : ncEmTaskRecordList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncEmTaskRecord);
                ncEmTaskRecord.setCreateBy(operName);
                ncEmTaskRecord.setUpdateBy(operName);
                this.insertNcEmTaskRecord(ncEmTaskRecord);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
